/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admintool.imp03_data;

import admintool.AdminToolView;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author Administrator
 */
public class IMP03_Experience {

    private class Experience {

        private int experienceID = 0;
        private int candidateID = 0;
        private String companyName = "";
        private String roleDesc = "";
        private int durationInYears = 0;
        
        public Experience(int experienceID, int candidateID, String companyName, String roleDesc, int durationInYears) {
            this.experienceID = experienceID;
            this.candidateID = candidateID;
            this.companyName = companyName;
            this.roleDesc = roleDesc;
            this.durationInYears = durationInYears;
        }

        public int getCandidateID() {
            return candidateID;
        }

        public String getCompanyName() {
            return companyName;
        }

        public int getDurationInYears() {
            return durationInYears;
        }

        public int getExperienceID() {
            return experienceID;
        }

        public String getRoleDesc() {
            return roleDesc;
        }
    }
    
    private static ArrayList<IMP03_Experience> experiences = new ArrayList<IMP03_Experience>();

    /**
     * 
     * @return
     */
    public static IMP03_Experience getEmptyExperienceObject(){
        return new IMP03_Experience(-1, -1, AdminToolView.EMPTY, AdminToolView.EMPTY, 0);
    }
    
    /**
     * 
     * @param reload
     * @param candidateID
     * @return
     */
    public static ArrayList<IMP03_Experience> getAllAvailableExperienceFromDB(boolean reload, int candidateID){
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            if(reload == true || experiences.isEmpty()){
                experiences.clear();
                ps = AdminToolView.getDBConnection().prepareStatement("SELECT * FROM experience WHERE CandidateID = ?");
                ps.setInt(1, candidateID);
                rs = ps.executeQuery();
                while(rs.next()){
                    experiences.add(new IMP03_Experience(rs.getInt(1), rs.getInt(2), rs.getString(3), rs.getString(4), rs.getInt(5)));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(rs != null){
                    rs.close();
                }
                if(ps != null){
                    ps.close();
                }
            } catch (Exception e) {
            }
        }
        return experiences;
    }
    
    private Experience experience = null;
    
    /**
     * 
     * @param experienceID
     * @param candidateID
     * @param companyName
     * @param roleDesc
     * @param durationInYears
     */
    public IMP03_Experience(int experienceID, int candidateID, String companyName, String roleDesc, int durationInYears) {
        this.experience = new Experience(experienceID, candidateID, companyName, roleDesc, durationInYears);
    }
    
    /**
     * 
     * @return
     */
    public int getExperienceID(){
        return experience.getExperienceID();
    }

    /**
     * 
     * @return
     */
    public int getExperienceDurationInYears(){
        return experience.getDurationInYears();
    }
    
    @Override
    public String toString() {
        return experience.getDurationInYears() + " years as a '" + experience.getRoleDesc() + "' at '" + experience.getCompanyName() + "'";
    }
}
